package com.online.shopping.service.impl;

import com.online.shopping.custom.Specification;
import com.online.shopping.mapper.TbSpecificationMapper;
import com.online.shopping.mapper.TbSpecificationOptionMapper;
import com.online.shopping.pojo.TbSpecification;
import com.online.shopping.pojo.TbSpecificationOption;
import com.online.shopping.pojo.TbSpecificationOptionExample;
import com.online.shopping.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper specificationMapper;
	@Autowired
	private TbSpecificationOptionMapper specificationOptionMapper;

	@Override
	public List<TbSpecification> findAllTbSpecification() {
		return specificationMapper.selectByExample(null);
	}

	@Override
	public Specification findSpecificationById(Long id) {
		Specification specification = new Specification();
		specification.setSpecification(specificationMapper.selectByPrimaryKey(id));

		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(id);

		specification.setSpecificationOptionList(specificationOptionMapper.selectByExample(example));
		return specification;
	}

	@Override
	public void add(Specification specification) {
		// 保存规格   一方数据
		specificationMapper.insert(specification.getSpecification());
		System.out.println("回显id："+specification.getSpecification().getId());
		// 保存规格选项   多方数据
		for(TbSpecificationOption option : specification.getSpecificationOptionList()){
			// 设置规格的ID：主键回填
			option.setSpecId(specification.getSpecification().getId());

			specificationOptionMapper.insert(option);
		}
	}

	@Override
	public void update(Specification specification) {
		// 修改规格   一方数据
		specificationMapper.updateByPrimaryKey(specification.getSpecification());

		// 先删除规格选项，再添加规格选项
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();

		criteria.andSpecIdEqualTo(specification.getSpecification().getId());
		specificationOptionMapper.deleteByExample(example);

		// 保存规格选项
		for(TbSpecificationOption option : specification.getSpecificationOptionList()){
			option.setSpecId(specification.getSpecification().getId());
			specificationOptionMapper.insert(option);
		}
	}

	@Override
	public void delete(Long[] ids) {
		// 先删除规格选项，再删除规格
		for(Long id : ids){
			TbSpecificationOptionExample example = new TbSpecificationOptionExample();
			TbSpecificationOptionExample.Criteria criteria = example.createCriteria();

			criteria.andSpecIdEqualTo(id);
			specificationOptionMapper.deleteByExample(example);

			specificationMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public List findSpecList() {
		return specificationMapper.selectOptionList();
	}
}
